//
//  HomeViewController.m
//  TaurusXi
//
//  Created by xicheng on 15/11/27.
//  Copyright © 2015年 xicheng. All rights reserved.
//

#import "HomeViewController.h"
#import "MainViewController.h"
#import "MyViewController.h"
#import "HomeBottomView.h"
#import "BindCardTipsViewController.h"
#import "UserHelper.h"
#import "LoginViewController.h"
#import "WebViewController.h"
#import "GTMBase64.h"
#import "NSString+URLDecode.h"


static const int kScrollViewTag = 10212;
@interface HomeViewController ()<HomeBottomChangeDelegate>
@property(nonatomic, strong) MainViewController *mainViewController;
@property(nonatomic, strong) MyViewController *myViewController;
@property(nonatomic,strong) UIScrollView *scrollerView;
@property(nonatomic,strong) HomeBottomView *bottomView;
@end

@implementation HomeViewController


+ (instancetype)sharedHomeViewController
{
  static HomeViewController *sharedInstance = nil;
  static dispatch_once_t onceToken = 0;
  dispatch_once(&onceToken, ^{
    sharedInstance = [self new];
  });
  return sharedInstance;
}


#pragma mark -生命周期
- (void)viewDidLoad {
  [super viewDidLoad];
  [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:YES];
  [self initView];
}

- (void)viewWillAppear:(BOOL)animated {
  [super viewWillAppear:animated];
}

- (void)viewDidLayoutSubviews {
  [super viewDidLayoutSubviews];
}

- (void)initView {
  self.navigationController.navigationBarHidden = YES;
  // init ScrollView
  [self.view addSubview:self.scrollerView];
  [self.view addSubview:self.bottomView];
  
}


#pragma mark - 懒加载

-(UIScrollView *)scrollerView{

  if(_scrollerView == nil){
    CGFloat scrollerViewHeight = self.deviceHeight - proxySize(kTabBarHeight);
    CGRect scrollerViewFrame =
    CGRectMake(0, 0, self.deviceWidth, scrollerViewHeight);
    UIScrollView *scrollerView =
    [[UIScrollView alloc] initWithFrame:scrollerViewFrame];
    _scrollerView = scrollerView;
    scrollerView.contentSize =
    CGSizeMake(self.deviceWidth * 2, scrollerViewHeight);
    scrollerView.scrollEnabled = NO;
    scrollerView.pagingEnabled = YES;
    scrollerView.showsVerticalScrollIndicator = NO;
    scrollerView.showsHorizontalScrollIndicator = NO;
    scrollerView.bounces = YES;
    
    // 加入 MainViewController
    {
      
//      UINavigationController *uiNavigationController = 
      self.mainViewController = [[MainViewController alloc] init];
//      self.mainViewController.view.frame =
//      CGRectMake(0, 0, self.deviceWidth, scrollerViewHeight);
      [scrollerView addSubview:self.mainViewController.view];
    }
    
    // 加入 MyViewController
    {
      self.myViewController = [[MyViewController alloc] init];
      self.myViewController.view.frame =
      CGRectMake(self.deviceWidth, 0, self.deviceWidth, scrollerViewHeight);
      [scrollerView addSubview:self.myViewController.view];
      
    }
    scrollerView.tag = kScrollViewTag;

  }

  return _scrollerView;
}

- (HomeBottomView *) bottomView{
  if (_bottomView ==nil) {
    _bottomView = [[HomeBottomView alloc] initWithFrame:CGRectMake(0, self.deviceHeight - proxySize(kTabBarHeight), self.deviceWidth, proxySize(kTabBarHeight))];
    _bottomView.homeBottomChangeDelegate = self;
  }
  return _bottomView;
}


#pragma mark 内存紧张
- (void)didReceiveMemoryWarning {
  [super didReceiveMemoryWarning];
  // Dispose of any resources that can be recreated.
}

-(void)pushVC:(UIViewController *)target{

  [self.navigationController pushViewController:target animated:YES];
}


#pragma mark - homeBottomChangeDelegate

- (void) selectMainAction{
  [UIView animateWithDuration:0.2 animations:^{
    self.scrollerView.contentOffset = CGPointMake(0, 0);
  }];
  
}

- (Boolean) selectMyAction{
  
  Boolean isLoginFlag = [[UserHelper shareUserInstance] isLogin];
  
  if (isLoginFlag) {
    [UIView animateWithDuration:0.2 animations:^{
      self.scrollerView.contentOffset = CGPointMake(self.deviceWidth, 0);
    }];
    return YES;
  }else{
    LoginViewController *target =[[LoginViewController alloc]initWithNibName:@"LoginViewController" bundle:nil
                                  ];
    [self.navigationController pushViewController:target animated:YES];
    
    return NO;
  }
}

+(void) pushViewController:(UIViewController *)viewController withAnimaiton:(Boolean)flag{
  
  [[HomeViewController sharedHomeViewController].navigationController pushViewController:viewController animated:flag];

}

+(void) popViewControllerWithAnimaiton:(Boolean)flag{
  
  [[HomeViewController sharedHomeViewController].navigationController popViewControllerAnimated:flag];
  
}

-(void) openURL:(NSURL *)url{
 
  NSString *path = [url absoluteString];
  NSLog(@"webviewPath:%@",path);
  
  if ([path containsString:@"webview?"]) {
    
    NSArray *array = [path componentsSeparatedByString:@"?"];
    if (array&&array.count>=2) {
      NSString *data = array[1];
      NSArray *dataArray = [data componentsSeparatedByString:@"&"];
      
      NSString *url = nil;
      NSString *title = nil;
      BOOL allSelectFlag = NO;
      if (dataArray&&dataArray.count>0) {
//        NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
        NSUInteger count = dataArray.count;
        for(NSUInteger i=0;i<count;i++) {
          NSString *str = dataArray[i];
          
          if ([str hasPrefix:@"url="]) {
            url = [str substringFromIndex:@"url=".length];
            url = [url stringByReplacingOccurrencesOfString:@"-" withString:@"+"];
            url = [url stringByReplacingOccurrencesOfString:@"_" withString:@"/"];
            url = [url stringByReplacingOccurrencesOfString:@"~" withString:@"="];
            url = [GTMBase64 decodeBase64String:url];
            url = [NSString URLDecodeString:url];
          
          }else if ([str hasPrefix:@"reloadPre="]){
          
          }else if ([str hasPrefix:@"title="]){
            title = [str substringFromIndex:@"title=".length];
            NSLog(@"title:%@",title);

          }else if ([str hasPrefix:@"canSelect="]){
            
          }
 
        }
      }
      WebViewController *webVC = [[WebViewController alloc]init];
      webVC.url = url;
      webVC.title = title;
      [self pushVC:webVC];
    }
  }
  
}

//URLDEcode
-(NSString *)decodeString:(NSString*)encodedString

{
  //NSString *decodedString = [encodedString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding ];
  
  NSString *decodedString  = (__bridge_transfer NSString *)CFURLCreateStringByReplacingPercentEscapesUsingEncoding(NULL,
                                                                                                                   (__bridge CFStringRef)encodedString,
                                                                                                                   CFSTR(""),
                                                                                                                   CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding));
  return decodedString;
}

@end
